package action.admin.account;

import dao.CheckAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class editAcc extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");
            if (action.equals("delete")) {
                String acc_id = request.getParameter("acc_id");
                String roll = request.getParameter("roll");
                CheckAccount ck = new CheckAccount();
                String msg = "";
                String error = "";
                if (ck.deleteAccount(acc_id, roll)) {
                    msg = "Delete successful !";
                } else {
                    error = "Delete fail !";
                }
                request.setAttribute("msg", msg);
                request.setAttribute("error", error);
                request.setAttribute("roll", roll);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=account");
                rd.forward(request, response);
            } else if (action.equals("update")) {
                String acc_id = request.getParameter("acc_id");
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String full_name = request.getParameter("full_name");
                String gender = request.getParameter("gender");
                String address = request.getParameter("address");
                String phone = request.getParameter("phone");
                String email = request.getParameter("email");
                String roll = request.getParameter("roll");                
                request.setAttribute("acc_id", acc_id);
                request.setAttribute("username", username);
                request.setAttribute("password", password);
                request.setAttribute("full_name", full_name);
                request.setAttribute("gender", gender);
                request.setAttribute("address", address);
                request.setAttribute("phone", phone);
                request.setAttribute("email", email);
                request.setAttribute("roll", roll);               
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=edituser");
                rd.forward(request, response);
            } else if (action.equals("show_by_roll")) {
                String roll = request.getParameter("roll");
                request.setAttribute("roll", roll);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=account");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
